package cn.catold.sspay;

import java.util.*;

import java.io.*;

import javax.servlet.http.HttpServletRequest;

import cn.beecloud.*;
import cn.beecloud.BCEumeration.*;
import cn.beecloud.bean.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by catold on 2018/2/1.
 */
@RestController
@RequestMapping("pay")
public class PayController {
    public static final String BASE_URL = "http://localhost:8080/";
    public static final String ALI_RETURN_URL = "return/ali";
    public static final String WX_RETURN_URL = "return/wx";

    static {
        BeeCloud.registerApp(Config.APP_ID, Config.TEST_SECRET,
            Config.APP_SECRET, Config.MASTER_SECRET);
    }

    @RequestMapping(value = "ali", method = RequestMethod.GET)
    public String alipay(HttpServletRequest request) {
        String result = "";
        //模拟商户的交易编号、标题、金额、附加数据
        String billNo = BCUtil.generateRandomUUIDPure();
        String title = "demo测试";
        //    String couponId = "dd7b827a-c452-41c2-987c-8d130fc5af20";
        String userId = "aa2@bc.com";
        Map<String, Object> optional = new HashMap<String, Object>();
        optional.put("rui", "feng");

        PAY_CHANNEL channel = PAY_CHANNEL.ALI_WAP;
        boolean success = false;

        BCOrder bcOrder = new BCOrder(channel, 1, billNo, title);
        BCInternationlOrder internationalOrder = new BCInternationlOrder();
        bcOrder.setBillTimeout(360);
        bcOrder.setOptional(optional);
        bcOrder.setBuyerId(userId);
        bcOrder.setReturnUrl(BASE_URL + ALI_RETURN_URL);

        try {
            bcOrder = BCPay.startBCPay(bcOrder);
            System.out.println(bcOrder.getObjectId());
            //Thread.sleep(3000);

            result = bcOrder.getHtml();
            System.out.println(result);

        } catch (BCException e) {
            System.out.println(e.getMessage());
        }

        return result;
    }

    //用户发起付款请求
    //系统生成付款订单，包括订单号，订单标题，金额等信息
    //将订单存入自己系统数据库中，标记订单为未支付
    //调用BeeCloud SDK中的支付接口，请求微信
    //    微信返回一个url或者html
    //通过跳转到url或者将html输出到页面进而打开微信的跳转中转页页面，打开微信APP实现收款
    //支付完成，用户跳转到设置的return url地址
    //支付成功，webhook通知商户服务器，商户校验后将自己数据库中的订单标记为支付成功
    @RequestMapping(value = "/wx", method = RequestMethod.GET)
    public String wx(HttpServletRequest request) {
        String result = "";
        //模拟商户的交易编号、标题、金额、附加数据
        String billNo = BCUtil.generateRandomUUIDPure();
        String title = "demo测试";
        //    String couponId = "dd7b827a-c452-41c2-987c-8d130fc5af20";
        String userId = "aa2@bc.com";
        Map<String, Object> optional = new HashMap<String, Object>();
        optional.put("rui", "feng");

        PAY_CHANNEL channel = PAY_CHANNEL.BC_WX_WAP;
        boolean success = false;

        BCOrder bcOrder = new BCOrder(channel, 1, billNo, title);
        BCInternationlOrder internationalOrder = new BCInternationlOrder();
        bcOrder.setBillTimeout(360);
        bcOrder.setOptional(optional);
        bcOrder.setBuyerId(userId);
        bcOrder.setReturnUrl(BASE_URL + WX_RETURN_URL);

        try {
            bcOrder.setTotalFee(1);
            Map<String, Object> analysisMap = new HashMap<>();
            analysisMap.put("ip", request.getRemoteAddr());
            bcOrder.setAnalysis(analysisMap);
            bcOrder = BCPay.startBCPay(bcOrder);

            result = "<a href='" + bcOrder.getUrl() + "'>pay</a>";
            success = true;

        } catch (BCException e) {
            System.out.println(e.getMessage());
        }

        return result;
    }
}
